﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Isuka.Excel;
using System.Collections.Specialized;

namespace isuka.samples.excel01
{
    public class ObservableExcelRowsCollection<TRow>
        : ObservableCollection<TRow>
    {
        public ObservableExcelRowsCollection()
        {

        }

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {

            }
            base.OnCollectionChanged(e);
        }
    }


    public class MainWindowViewModel
        : IDisposable
    {
        XApplication application;
        XWorksheet sheet;

        public MainWindowViewModel()
        {
            application = new XApplication()
            {
                Visible = true
            };
            try
            {
                sheet = application.Workbooks.CreateNew().Worksheets[0];

                var begin = sheet[0, 0];
                var end = sheet[4, 4];
                var dynRows = sheet.Range(begin, end).ToDynamicRows();
                var data = dynRows
                            .Select(x => new SampleDataModel(sheet, x))
                            .DefaultIfEmpty(new SampleDataModel(sheet, dynRows.First()))
                            ;
                XData = new ObservableCollection<ISampleDataModel>(data);
            }
            catch (Exception)
            {
                application.Dispose();
                throw;
            }
        }
        public ObservableCollection<ISampleDataModel> XData { get; private set; }

        #region IDisposable メンバー

        public void Dispose()
        {
            application.Dispose();
        }

        #endregion
    }
}
